<template>
  <div ref="polylineRef" style="width: 100%; height: 100%"></div>
</template>
<script lang="ts" setup>
import { ref, onMounted, computed, watchEffect } from "vue";
//  按需引入 echarts
import * as echarts from "echarts";
const polylineRef = ref(null) as any;
onMounted(() => {
  // init();
});
const props = defineProps({
  title: {
    type: String,
    default: "",
  },
  data: {
    type: Array,
    default: () => [],
  },
});
const xAxisList = computed(() =>
  props.data.map((obj: any) => obj.dt || obj.date)
);
const dataList = computed(() => props.data.map((obj: any) => obj.value));
watchEffect(() => {
  if (xAxisList.value.length > 0 && polylineRef.value) {
    init();
  }
});

function init() {
  // 基于准备好的dom，初始化echarts实例
  var myChart = echarts.init(polylineRef.value);
  // 指定图表的配置项和数据
  var option = {
    title: {
      text: props.title,
    },
    tooltip: {
      trigger: "axis",
    },
    // legend: {
    //   data: ["Email", "Union Ads", "Video Ads", "Direct", "Search Engine"],
    // },
    grid: {
      top: "18%",
      left: "3%",
      right: "3%",
      bottom: "3%",
      containLabel: true,
    },
    xAxis: {
      type: "category",
      boundaryGap: false,
      data: xAxisList.value,
    },
    yAxis: {
      type: "value",
    },
    series: [
      {
        type: "line",
        data: dataList.value,
      },
    ],
  };
  // 使用刚指定的配置项和数据显示图表。
  myChart.setOption(option);
}
</script>
<style scoped></style>
